package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Permission;
import com.itheima.POJO.Role;
import com.itheima.POJO.User;
import com.itheima.VO.UserRoleIDS;
import com.itheima.VO.User_ids;
import com.itheima.dao.PermissionMapper;
import com.itheima.dao.RoleMapper;
import com.itheima.dao.UserMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
   private PermissionMapper permissionMapper;
    public User findUserByusername(String username) {
        //通过用户名 到数据库查到用户信息 包括角色和权限信息

        //1. 根据用户名 查基本信息
        User userByusername = userMapper.findUserByusername(username);
        if (userByusername == null){
            return  null;
        }
        //2.通过用户id 查询角色的set集合
        Integer id = userByusername.getId();
        Set<Role> roles = roleMapper.findRoleByuserid(id);

        for (Role role : roles) {
            //3.遍历每一个set集合 根据角色id 查询权限集合
            Integer roleId = role.getId();
            Set<Permission> permissionSet = permissionMapper.findBYroleID(roleId);
            role.setPermissions(permissionSet);
        }


        //4.设置用户角色集合  每一个角色里面有权限集合
        userByusername.setRoles(roles);
        return userByusername;
    }

    @Override
    public PageResult findAllUser(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //下面这步其实多此一举 因为前端定义当前页默认为1 了!
        if (currentPage==null||currentPage.equals("")){
            currentPage=1;
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page=userMapper.findPage(queryString);
        PageResult pageResult=new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void addUser(User_ids user_ids) {

        try {
            User user = user_ids.getFormData();
            String password = MD5Utils.md5(user.getPassword());
            user.setPassword(password);
            userMapper.addUser(user);
            Integer id = user.getId();
            Integer[] ids = user_ids.getCheckitemIds();
            if (ids !=null){
                for (Integer roleid : ids) {
                    userMapper.adduser_roleByuserID(id,roleid);
                }
            }
        } catch (RuntimeException e) {
            throw  new RuntimeException("新建用户失败");
        }

    }

    @Override
    public void addUserRoleS(UserRoleIDS userRoleIDS) {
        try {
            Integer userid = userRoleIDS.getUserid();
            Integer[] roleids = userRoleIDS.getRoleids();
            if (roleids !=null){
                for (Integer roleid : roleids) {
                    userMapper.adduser_roleByuserID(userid,roleid);
                }
            }
        } catch (RuntimeException e) {
            throw new RuntimeException("添加角色失败");
        }
    }

    @Override
    public List<User> findAllHealthManager() {
        List<User> users = userMapper.findAllHealthManager();
        return users;
    }
}
